﻿CREATE proc [log].[SaveLog]
@UserID int,
@LogType nchar(10),
@LogArea nvarchar(50),
@LogDetails nvarchar(max),
@IsError bit,
@LogDateTime datetime2(7) = null,
@ComputerName nvarchar(50) = null,
@DllVersion nvarchar(50) = null

as
begin
	declare @errorMsg nvarchar(150),
			@logDate datetime2(7)

	if @LogType is null
	begin
		set @errorMsg = '@LogType is null or empty.'
		goto WithError
	end

	if @LogArea is null
	begin
		set @errorMsg = '@LogArea is null or empty.'
		goto WithError
	end

	if @LogDetails is null
	begin
		set @errorMsg = '@LogDetails is null or empty.'
		goto WithError
	end

	if @IsError is null
	begin
		set @errorMsg = '@IsError is null or empty.' 
		goto WithError
	end
	
	set @logDate = @LogDateTime

	if @LogDateTime is null
	begin
		set @logDate = sysdatetime()
	end

	--begin tran

	insert into [log].hlpLog (
				UserID,
				LogType,
				LogArea,
				LogDateTime,
				LogDetails,
				IsError,
				ComputerName,
				DllVersion
				)
	values	(	@UserID,
				@LogType,
				@LogArea,
				@logDate,
				@LogDetails,
				@IsError,
				@ComputerName,
				@DllVersion
			)
				
	--commit

	return 0

WithError:
	
	RAISERROR
    (@errorMsg,
    10, -- Severity.
    1, -- State.
    @UserID, -- First substitution argument.
    @LogDetails); -- Second substitution argument. 
	return -1 
end

GO


